<template>
  <div class="app-container">
    <div class="view-title">轮播图管理</div>
    <el-button
      type="primary"
      size="mini"
      @click="handlerAddSwiper"
      v-permission="['swiperUpload:add']"
      >新增轮播图</el-button
    >

    <el-table
      :data="swiperTableData"
      v-loading="loading"
      element-loading-text="正在加载中"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="name" label="名称" width="180" align="center">
      </el-table-column>
      <el-table-column prop="path" label="图片链接" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.path" alt="" style="width: 170px" />
        </template>
      </el-table-column>
      <el-table-column label="是否展示" align="center">
        <template v-slot="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="0"
            :inactive-value="1"
            @change="upEnabled($event, row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="权重" width="80" align="center">
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="创建时间"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="updated_at"
        label="更新时间"
        width="200"
        align="center"
      >
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="160" align="center">
        <template slot-scope="scope">
          <el-button
            @click="handleCheck(scope.row)"
            type="text"
            size="small"
            v-permission="['swiperUpload:view']"
            >查看</el-button
          >
          <el-button
            @click="handleEdit(scope.row)"
            type="text"
            size="small"
            v-permission="['swiperUpload:edit']"
            >修改</el-button
          >
          <el-button
            @click="handleDelete(scope.row)"
            type="text"
            size="small"
            v-permission="['swiperUpload:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--      分页-->
    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!--      新增弹窗-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisibleAdd"
      width="30%"
      :close-on-click-modal="false"
      @close="handleCloseDialog"
    >
      <div class="dialog_swiper_box">
        <el-form
          :model="ruleFormSwiper"
          :rules="rules"
          ref="swiperRuleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleFormSwiper.name"></el-input>
          </el-form-item>

          <el-form-item label="图片路径" prop="path">
            <el-upload
              ref="myUpload"
              style="margin-right: 30px"
              class="upload-demo"
              accept=".jpeg,.png,.jpg,.bmp,.gif"
              :action="uploadMainImgUrl"
              :on-success="onSuccessFile"
              :on-error="onErrorFile"
              :data="{ type: '4' }"
              list-type="text"
            >
              <el-button size="small" type="primary">上传素材</el-button>
            </el-upload>
            <el-input v-model="ruleFormSwiper.path"></el-input>
          </el-form-item>

          <el-form-item label="跳转链接" prop="url">
            <el-input v-model="ruleFormSwiper.url"></el-input>
          </el-form-item>

          <el-form-item label="轮播图类型" prop="type">
            <el-input v-model="ruleFormSwiper.type"></el-input>
          </el-form-item>

          <el-form-item label="权重" prop="weight">
            <el-input v-model="ruleFormSwiper.weight"></el-input>
          </el-form-item>
          <el-form-item label="是否展示">
            <el-switch
              v-model="ruleFormSwiper.status"
              :active-value="0"
              :inactive-value="1"
            >
            </el-switch>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handlerCancel">取 消</el-button>
        <el-button type="primary" @click="handlerSure('swiperRuleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!--      查看弹窗-->
    <el-dialog
      title="查看轮播图"
      :visible.sync="dialogVisibleCheckSwiper"
      width="30%"
    >
      <div class="check_img_box">
        <img :src="checkImg" alt="" class="check_img" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAppBannerList,
  addAppBanner,
  deleteAppBanner,
  updateAppBanner,
  getAppBannerInfo,
} from "@/api/swiperUpload.js";

export default {
  name: "SwiperUpload",
  data() {
    return {
      pageNow: 1,
      pageSize: 10,
      total: null,
      swiperTableData: [],
      dialogVisibleAdd: false,
      uploadMainImgUrl: "https://app.server.jkcae.com/app/uploadFiles", // 主图url
      ruleFormSwiper: {
        name: "",
        path: "",
        type: "",
        url: "",
        weight: "",
        status: 0,
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        path: [
          { required: true, message: "请输入上传轮播图", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择轮播图类型", trigger: "blur" },
        ],
        weight: [{ required: true, message: "请输入权重", trigger: "blur" }],
      },
      deleteId: "",
      editId: "",
      checkId: "",
      dialogVisibleCheckSwiper: false,
      checkImg: "",
      loading: true,
      dialogTitle: "",
    };
  },
  mounted() {
    this.getAppBannerList();
  },
  methods: {
    // 获取轮播图列表
    getAppBannerList() {
      getAppBannerList({
        page_now: this.pageNow,
        page_size: this.pageSize,
      }).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.swiperTableData = res.data;
          this.total = res.page_info.total_count;
        } else {
          this.loading = false;
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 上传轮播图
    addAppBanner() {
      addAppBanner(this.ruleFormSwiper).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogVisibleAdd = false;
          this.getAppBannerList();
          this.ruleFormSwiper = {};
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 删除轮播图
    deleteAppBanner() {
      deleteAppBanner({
        ids: this.deleteId,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogVisibleAdd = false;
          this.getAppBannerList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 修改轮播图
    updateAppBanner() {
      updateAppBanner(this.ruleFormSwiper).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogVisibleAdd = false;
          this.getAppBannerList();
          this.ruleFormSwiper = {};
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 查看轮播图
    getAppBannerInfo() {
      this.checkImg = "";
      getAppBannerInfo({
        id: this.checkId,
      }).then((res) => {
        if (res.code == 1) {
          this.checkImg = res.data.path;
          this.dialogVisibleCheckSwiper = true;
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 上传主图
    onSuccessFile(val) {
      if (val.code == 1) {
        this.$message.success(val.msg);
        this.ruleFormSwiper.path = val.data.path;
        // this.dialogVisibleAddMaterials = false
      } else {
        this.$message.error(val.msg);
        return;
      }
    },
    onErrorFile(val) {},

    // 新增弹窗-叉号
    handleCloseDialog() {
      this.$refs.myUpload.clearFiles();
      this.dialogVisibleAdd = false;
      this.ruleFormSwiper = this.$options.data().ruleFormSwiper;
    },

    // 新增轮播图
    handlerAddSwiper() {
      this.editId = null;
      this.dialogTitle = "新增";
      this.dialogVisibleAdd = true;
    },

    // 新增弹窗确定
    handlerSure(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId) {
            this.updateAppBanner();
          } else {
            this.addAppBanner();
          }
        } else {
          return false;
        }
      });
    },

    // 新增弹窗取消
    handlerCancel() {
      this.dialogVisibleAdd = false;
      this.ruleFormSwiper = {};
    },

    // 查看
    handleCheck(val) {
      this.checkId = val.id;
      this.getAppBannerInfo();
    },

    // 修改
    handleEdit(val) {
      this.dialogTitle = "编辑";
      this.ruleFormSwiper = val;
      this.editId = val.id;
      this.dialogVisibleAdd = true;
    },

    // 删除
    handleDelete(val) {
      this.deleteId = val.id;
      this.$confirm("此操作将删除当前轮播图, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteAppBanner();
        })
        .catch(() => {});
    },
    upEnabled(e, val) {
      this.$confirm("是否更改展示状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateAppBanner({
            id: val.id,
            status: e,
          })
            .then((res) => {
              if (res.code == 1) {
                this.$message.success("修改成功");
                this.getAppBannerList();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.getAppBannerList();
            });
        })
        .catch(() => {
          this.getAppBannerList();
        });
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAppBannerList();
    },
    handleCurrentChange(val) {
      this.pageNow = val;
      this.pageSize = 10;
      this.getAppBannerList();
    },
  },
};
</script>

<style scoped lang="scss">
// .swiper_upload_container {
//   .swiper_upload_box {
//     .top_btn_box {
//       padding: 20px 0 20px 20px;
//     }

//     .table_box {
//       padding: 0 20px;

//       .table_img {
//         display: inline-block;
//         height: auto;
//         max-width: 100%;
//       }
//     }

//     .page_box {
//       display: flex;
//       justify-content: center;
//       padding: 20px 0;
//     }
//   }
// }
.check_img_box {
  width: auto;
  height: auto;

  .check_img {
    width: 100%;
    height: 100%;
  }
}
</style>
